﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Utility.Data;
using System.Utility;
using System.Utility.DBMS;
using System.Data;
using GuessLAB.UI.Classes;
using GuessLAB.Domain;
using System.Utility.UI;

namespace GuessLAB
{
    public partial class UpdatePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //lblMessage.Visible = false;

            //if (!ValidateRequest())
            //    Response.Redirect("Login.aspx", true);

        }

        private bool ValidateRequest()
        {
            bool validRequest = false;
            try
            {
                string encryptId = Request.QueryString["UID"];
                if (Session[encryptId] != null)
                {
                    validRequest = true;
                    Logger.LogMessage("Update password request has been mode for userid - " + encryptId, LogLevel.INFO, LogMessageType.WebMessage);
                }
            }
            catch (Exception ex)
            { validRequest = false; }
            return validRequest;
        }

        private void updatePasswordRequest(long userid, string password)
        {
            try
            {

                DataSet set = DBHelper.GetDataTable("UpdateUserPassword", new object[] { userid, password });

                if (set != null && set.Tables.Count > 0)
                {
                    DataTable table = set.Tables[0];

                    if (table != null && table.Rows.Count > 0)
                    {
                        string pageurl = null;
                        AppUser  user = new AppUser();
                        DBHelper.PopulateObject(user, table.Rows[0]);
                        user.Type = AppUserType.Admin;
                        StateManager<AppUser>.SetSessionState(SessionKey.UserInfo, user);
                        switch (user.Type)
                        {
                            case AppUserType.Admin:
                                pageurl = "Home.aspx";
                                break;
                        }
                        Response.Redirect(pageurl);

                    }
                }
                else
                Response.Redirect(Constant.LoginUrl, false);

            }
            catch (Exception ex)
            {
                Response.Redirect("Home.aspx", false);
            }

        }


        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Logger.LogMessage("Before updating the password ", LogLevel.INFO, LogMessageType.WebMessage);


            string encryptId = Request.QueryString["UID"];
            long userId = long.Parse(Session[encryptId].ToString());

            Logger.LogMessage("Encrypt id -  " + encryptId, LogLevel.INFO, LogMessageType.WebMessage);

            //Util.LogRequest("update password request has been made for the user " + userId.ToString(), this.Request, null);
            int retLogin = -1;
            Logger.LogMessage("Updating password in database", LogLevel.INFO, LogMessageType.WebMessage);
            updatePasswordRequest(userId, DataSecurity.Encrypt(passwordsignup.Value.Trim(), true));

            //if (retLogin == 0)
            //{
                //Session.Add("PasswordChanged", "Password Changed Successfully.");
                //Session.Timeout = int.Parse(Settings.GetSettingByKey("SessionLimit"));
                // Server.Transfer()
                Logger.LogMessage("Password Changed Successfully and redirecting to the login page", LogLevel.INFO, LogMessageType.WebMessage);
                Response.Redirect("Loginpage.aspx", false);
            //}
            //else if (retLogin != 200)
            //{
            //    lblMessage.Text = "Could not Update Password . Please try again.";
            //    lblMessage.Visible = true;
            //}

            //else if (retLogin == 200)
            //{
            //    //lblMessage.Text = "Could not Update Password . Please try again.";
            //    lblMessage.Visible = true;
            //}

        }
    }
}